package com.hdcircles.autoform.ui.main.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.hdcircles.autoform.R;
import com.hdcircles.autoform.app.App;
import com.hdcircles.autoform.app.Constants;
import com.hdcircles.autoform.base.BaseActivity;
import com.hdcircles.autoform.model.bean.FormBean;
import com.hdcircles.autoform.model.bean.User;
import com.hdcircles.autoform.presenter.MainPresenter;
import com.hdcircles.autoform.presenter.contract.MainContract;
import com.hdcircles.autoform.ui.label.fragment.LabelMainFragment;
import com.hdcircles.autoform.ui.main.fragment.AboutFragment;
import com.hdcircles.autoform.ui.main.fragment.UserFragment;
import com.hdcircles.autoform.util.SharedPreferenceUtil;
import com.hdcircles.autoform.util.SnackbarUtil;
import com.hdcircles.autoform.util.ToastUtil;
import com.melnykov.fab.FloatingActionButton;

import java.util.List;

import butterknife.BindView;
import cn.hugeterry.updatefun.UpdateFunGO;
import cn.hugeterry.updatefun.config.UpdateKey;
import me.yokeyword.fragmentation.SupportFragment;

public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tab_label_main)
    TabLayout tabLabelMain;
    @BindView(R.id.navigationView)
    NavigationView navigationView;
    @BindView(R.id.drawer)
    DrawerLayout drawer;
    @BindView(R.id.fab_add)
    FloatingActionButton fabAdd;

    private int hideFragment= Constants.TYPE_LABEL;
    private int showFragment=Constants.TYPE_LABEL;

    LabelMainFragment mLabelMainFragment;
    AboutFragment mAboutFragment;
    UserFragment mUserFragment;

    MenuItem mLastMenuItem;

    private List<FormBean> formBeanList;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    /**
     * 由于recreate 需要特许处理夜间模式
     *
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (null == savedInstanceState) {
            SharedPreferenceUtil.setNightModeState(false);
        } else {
            showFragment=SharedPreferenceUtil.getCurrentItem();
            hideFragment=Constants.TYPE_LABEL;
            showHideFragment(getTargetFragment(showFragment),getTargetFragment(hideFragment));
            navigationView.getMenu().findItem(R.id.drawer_label).setChecked(false);
            hideFragment=showFragment;
        }
    }

    @Override
    protected void initEventAndData() {
        setToolBar(toolbar,"全部");
        UpdateKey.API_TOKEN="065de0812cb544b223541b22f7a43e21";
        UpdateKey.APP_ID="5812f6b2959d69070200057d";
        UpdateFunGO.init(this);
        initFragment();
        ActionBarDrawerToggle drawerToggle=new ActionBarDrawerToggle(this,drawer,toolbar,R.string.drawer_open,R.string.drawer_close);
        drawerToggle.syncState();
        drawer.addDrawerListener(drawerToggle);
        mLastMenuItem=navigationView.getMenu().findItem(R.id.drawer_label);
        mLastMenuItem.setChecked(true);
        loadMultipleRootFragment(R.id.fl_main_content,0,mLabelMainFragment,mAboutFragment,mUserFragment);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if ("".equals(SharedPreferenceUtil.getUserName())){
                    ToastUtil.shortShow("请完善用户信息");
                    return true;
                }
                switch (item.getItemId()){
                    case R.id.drawer_label:
                        showFragment=Constants.TYPE_LABEL;
                        tabLabelMain.setVisibility(View.VISIBLE);
                        fabAdd.setVisibility(View.VISIBLE);
                        break;
                    case R.id.drawer_about:
                        showFragment=Constants.TYPE_ABOUT;
                        tabLabelMain.setVisibility(View.GONE);
                        fabAdd.setVisibility(View.GONE);
                        break;
                    case R.id.drawer_personal:
                        showFragment=Constants.TYPE_USER;
                        tabLabelMain.setVisibility(View.GONE);
                        fabAdd.setVisibility(View.GONE);
                        break;
                }
                if (null!=mLastMenuItem){
                    mLastMenuItem.setChecked(false);
                }
                mLastMenuItem=item;
                SharedPreferenceUtil.setCurrentItem(showFragment);
                item.setChecked(true);
                toolbar.setTitle(item.getTitle());
                drawer.closeDrawers();
                showHideFragment(getTargetFragment(showFragment),getTargetFragment(hideFragment));
                hideFragment=showFragment;
                return true;
            }
        });

       mPresenter.getUser(SharedPreferenceUtil.getUserEmail());

    }

    private void initFragment() {
        mLabelMainFragment=new LabelMainFragment();
        mAboutFragment=new AboutFragment();
        mUserFragment=new UserFragment();
    }

    @Override
    public void showError(String msg) {
        SnackbarUtil.showShort(toolbar,msg);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu,menu);
        return true;
    }

    @Override
    public void onBackPressedSupport() {
//        super.onBackPressedSupport();
        showExitDialog();
    }



    private void showExitDialog(){
        AlertDialog.Builder builder=new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("确定退出AirForm吗?")
                .setNegativeButton("取消",null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        App.getInstance().exitApp();
                    }
                });
        builder.show();
    }

    private SupportFragment getTargetFragment(int item){
        switch (item){
            case Constants.TYPE_LABEL:
                return mLabelMainFragment;
            case  Constants.TYPE_ABOUT:
                return mAboutFragment;
            case Constants.TYPE_USER:
                return mUserFragment;

        }

        return mLabelMainFragment;
    }

    public TabLayout getTabLabelMain() {
        return tabLabelMain;
    }

    public FloatingActionButton getFabAdd() {
        return fabAdd;
    }

    @Override
    public void showUser(User user) {
        if (null!=user.getName()){
            SharedPreferenceUtil.setUserName(user.getName());
        }
        if ("".equals(SharedPreferenceUtil.getUserName())){
            mLastMenuItem.setChecked(false);
            tabLabelMain.setVisibility(View.GONE);
            fabAdd.setVisibility(View.GONE);
            showFragment=Constants.TYPE_USER;
            hideFragment=Constants.TYPE_LABEL;
            showHideFragment(getTargetFragment(showFragment),getTargetFragment(hideFragment));
            navigationView.getMenu().findItem(R.id.drawer_personal).setChecked(true);
            mLastMenuItem=navigationView.getMenu().findItem(R.id.drawer_personal);
            toolbar.setTitle(mLastMenuItem.getTitle());
            hideFragment=showFragment;
        }

    }
}
